<template>
	<view>
		<view class="questionPage" v-for="(data,index) in QAlist" :key="index">
			<view class="left">
				<view class="avatar">
					<img loading="lazy" :src="data.imgPath">
				</view>
				<view class="name">
					{{data.stuName}}
				</view>
			</view>
			<view class="right">
				<view class="QAbox">
					<view class="QAcontent">
						<view class="top">
							<view class="QAtitle">
								{{data.title}}
							</view>
							<view class="time">
								{{data.time}}
							</view>
						</view>
						<view class="main">
							<view class="text">
								<u-parse :html="data.content"></u-parse>
							</view>
						</view>
						<view class="bottom">
							<view class="icons" v-if="data.isMy">
								<view class="edit" @click="edit(data)">
									<u-icon name="edit-pen-fill"></u-icon>编辑
								</view>
								<view class="delete" @click="deleteQA(data)">
									<u-icon name="trash-fill"></u-icon>删除
								</view>
								<view class="detail" @click="toDetail(data)">
									<u-icon name="chat-fill"></u-icon>回答{{data.totalQue}}
								</view>
							</view>
							<view class="icons" v-if="!data.isMy">
								<view class="detail" @click="toDetail(data)">
									<u-icon name="chat-fill"></u-icon>回答{{data.totalQue}}
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view>
			<u-popup v-model="popupEdit" mode="center" width="60%" class="popupEdit">
				<view>
					<view class="poptitle">
						<view class="poptitleTitle">
							标题
						</view>
						<view class="poptitleEditor">
							<editor id="titleeditor" class="ql-container" @input="getTitle" showImgToolbar @statuschange="onStatusChange"
							 :read-only=false @ready="onTitleEditorReady">
								{{editTitle}}
							</editor>
						</view>
					</view>
					<view class="poptext">
						<view class="poptextTitle">
							问题内容
						</view>
						<view class="poptextEditor">
							<editor id="texteditor" class="ql-container" @input="getText" showImgToolbar @statuschange="onStatusChange"
							 :read-only=false @ready="onTextEditorReady">
								{{editText}}
							</editor>
						</view>
					</view>
					<view class="editbutton">
						<button type="default" @click="editFinish()">完成</button>
					</view>
				</view>
			</u-popup>
		</view>
		<view>
			<u-popup v-model="popupDelete" mode="center">
				<view class="popupDelete">
					<view class="warnText">
						删除后不可恢复，确认删除吗？
					</view>
					<view class="confirmButton">
						<view class="deletebutton">
							<u-button type="error" @click="deleteFinish()">删除</u-button>
						</view>
						<view class="canclebutton">
							<u-button type="default" @click="popupDelete = false">取消</u-button>
						</view>
					</view>
				</view>
			</u-popup>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				popupEdit: false,
				popupDelete: false,
				editTitle: '',
				editText: '',
				editId: null,
				delingId: null
			};
		},
		props: {
			QAlist: {
				type: Array,
				default () {
					return []
				}
			}
		},
		methods: {
			onTextEditorReady() {
				var that = this;
				uni.createSelectorQuery().select('#texteditor').context((res) => {
					this.editorCtx = res.context;
					this.editorCtx.setContents({ //赋值
						html: that.editText
					});
				}).exec()
			},
			onTitleEditorReady() {
				var that = this;
				uni.createSelectorQuery().select('#titleeditor').context((res) => {
					this.editorCtx = res.context;
					this.editorCtx.setContents({ //赋值
						html: that.editTitle
					});
				}).exec()
			},
			getText(e) {
				var that = this;
				console.log(e.detail.html); //带标签内容
				console.log(e.detail.text); //纯文本内容
				that.editText = e.detail.text
			},
			getTitle(e) {
				var that = this;
				console.log(e.detail.html); //带标签内容
				console.log(e.detail.text); //纯文本内容
				that.editTitle = e.detail.text
			},
			edit(data) {
				this.popupEdit = true
				this.editId = data.id
				this.editTitle = data.title
				this.editText = data.content
			},
			editFinish() {
				if (!this.editTitle) {
					this.$refs.uToast.show({
						title: '标题不能为空',
						type: 'warning'
					})
					return
				}
				if (!this.editText) {
					this.$refs.uToast.show({
						title: '内容不能为空',
						type: 'warning'
					})
					return
				}
				this.$api.updateQuestions(this.$store.state.access_token, this.editId, this.editTitle, this.editText)
					.then(res => {
						console.log(res);
						this.$refs.uToast.show({
							title: '编辑成功',
							type: 'success'
						})
						setTimeout(() => {
							this.$emit('refreshQA')
						}, 1000)

					})
				this.popupEdit = false
			},
			deleteQA(data) {
				this.popupDelete = true
				this.delingId = data.id
			},
			deleteFinish() {
				this.$api.delQuestions(this.$store.state.access_token, this.delingId)
					.then(res => {
						console.log(res);
						this.delingId = null
						this.$refs.uToast.show({
							title: '删除成功',
							type: 'success'
						})
						setTimeout(() => {
							this.$emit('refreshQA');
						}, 1200)

					})
				this.popupDelete = false
			},
			toDetail(data) {
				this.$u.route('pages/course/QAdetail/QAdetail', data);
			}
		}
	}
</script>

<style lang="scss">
	.questionPage {
		display: flex;
		flex-direction: row;
		box-sizing: border-box;
		padding: 5rpx;

		.left {
			display: flex;
			flex-direction: column;
			align-items: center;
			box-sizing: border-box;

			image {
				width: 90rpx;
				height: 90rpx;
				border-radius: 50%;
				background-color: #f2f2f2;
			}
		}

		.right {
			box-sizing: border-box;
			width: 500rpx;

			.QAbox {
				box-sizing: border-box;
				padding: 10rpx 10rpx 10rpx 10rpx;
				box-sizing: border-box;
				margin: 10px;
				box-shadow: 0 0 5px 1px rgba($color: #000000, $alpha: 0.1);
				border-radius: 5px;
				width: 100%;
				background-color: white;

				.QAcontent {
					width: 100%;
					display: flex;
					flex-direction: column;
					display: -webkit-box;
					overflow: hidden;
					text-overflow: ellipsis;
					word-wrap: break-word;
					white-space: normal !important;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;

					.top {
						display: flex;
						justify-content: space-between;
						align-items: center;

						.QAtitle {
							font-weight: bold;
						}

						.time {
							color: #BED3ED;
						}
					}

					.main {
						box-sizing: border-box;
						padding: 10rpx 5rpx 10rpx 5rpx;
						width: 100%;

						.text {
							display: -webkit-box;
							overflow: hidden;
							text-overflow: ellipsis;
							word-wrap: break-word;
							white-space: normal !important;
							-webkit-line-clamp: 2;
							-webkit-box-orient: vertical;
						}
					}

					.bottom {
						display: flex;
						justify-content: space-between;

						.icons {
							color: #0074D1;
							display: flex;
							flex-direction: row;

							.edit {
								box-sizing: border-box;
								padding-right: 10rpx;
							}

							.delete {
								box-sizing: border-box;
								padding: 0 10rpx 0 10rpx;
								border-left: 1rpx solid black;
								border-right: 1rpx solid black;
							}

							.detail {
								box-sizing: border-box;
								padding-left: 10rpx;
							}
						}
					}
				}
			}
		}
	}

	.popupDelete {
		box-sizing: border-box;
		padding: 50rpx;

		.confirmButton {
			padding-top: 35rpx;
			box-sizing: border-box;
			display: flex;
			justify-content: space-evenly;

			.deletebutton {
				width: 45%;
			}

			.canclebutton {
				width: 45%;
			}

		}
	}

	.popupEdit {
		.poptitle {
			.poptitleTitle {
				font-size: 35rpx;
				font-weight: bold;
				box-sizing: border-box;
				padding: 5rpx 10rpx 5rpx 10rpx;
			}

			.poptitleEditor {
				box-sizing: border-box;
				padding: 0 0 0 20rpx;
			}
		}

		.poptext {
			.poptextTitle {
				font-size: 35rpx;
				font-weight: bold;
				box-sizing: border-box;
				padding: 5rpx 10rpx 0 10rpx;
			}

			.poptextEditor {
				box-sizing: border-box;
				padding: 0 0 0 20rpx;
			}
		}
	}

	.avatar {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50rpx;
		overflow: hidden;

		img {
			width: 100%;
			height: 100%;
		}
	}
</style>
